import Vue from 'vue';
import VueRouter from 'vue-router';

// 注册路由
Vue.use(VueRouter);

// 懒加载引入组件
const Layout = () => import('@/views');
// const Login = () => import('@/views/Login');

/**
 * 发现音乐
 */
const DiscoverMusic = () => import('@/views/DiscovrMusic');
const Recommendation = () => import('@/views/DiscovrMusic/Recommendation');
const SongMenu = () => import('@/views/DiscovrMusic/SongMenu');
const RankList = () => import('@/views/DiscovrMusic/RankList');
const Singer = () => import('@/views/DiscovrMusic/Singer');
const NewSongs = () => import('@/views/DiscovrMusic/NewSongs');

/**
 * 详情信息页
 */
const PlayDetail = () => import('@/views/playDetail');
const ArtistDetail = () => import('@/views/artistDetail');
const UserDetail = () => import('@/views/userDetail');
const AlbumDetail = () => import('@/views/albumDetail');
const VideoDetail = () => import('@/views/video/videoDetail');
const SearchDetail = () => import('@/views/searchDetail');

/**
 * 视频相关
 */
const Video = () => import('@/views/video');
const SubVideo = () => import('@/views/video/subItem/subVideo.vue');
const SubMv = () => import('@/views/video/subItem/subMv.vue');
const AllMv = () => import('@/views/video/allPage/allMv.vue');
const AllTop = () => import('@/views/video/allPage/allTop.vue');

/**
 * 私人FM
 */
const PrivateFM = () => import('@/views/Home/privateFM');

/**
 * 每日推荐
 */
const DailySpecial = () => import('@/views/Home/dailySpecial');

/**
 * 最近播放
 */
const RecentPlay = () => import('@/views/Home/recentPlay');

/**
 * 我的收藏
 */
const MyFavorite = () => import('@/views/Home/myFavorite');

// 重写push|replace
const originPush = VueRouter.prototype.push; // 保存路由原有的push方法
const originReplace = VueRouter.prototype.replace; // 保存路由原有的replace方法

VueRouter.prototype.push = function(to, resolve, reject) {
  // 将this指回VueRouter
  if (resolve && reject) {
    originPush.call(this, to, resolve, reject);
  } else {
    originPush.call(this, to, () => {}, () => {});
  }
}
VueRouter.prototype.replace = function(to, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, to, resolve, reject);
  } else {
    originReplace.call(this, to, () => {}, () => {});
  }
}

export default new VueRouter({
  routes: [
    {
      name: 'Layout',
      path: '/',
      redirect: '/discovermusic',
      component: Layout,
      children: [
        // 发现音乐
        {
          name: 'discovermusic',
          path: '/discovermusic',
          redirect: '/recommendation',
          component: DiscoverMusic,
          children: [{
              name: 'recommendation',
              path: '/recommendation',
              component: Recommendation,
              meta: {
                name: '个性推荐'
              }
            }, {
              name: 'songmenu',
              path: '/songmenu',
              component: SongMenu,
              meta: {
                name: '歌单'
              }
            }, {
              name: 'ranklist',
              path: '/ranklist',
              component: RankList,
              meta: {
                name: '排行榜'
              }
            }, {
              name: 'singer',
              path: '/singer',
              component: Singer,
              meta: {
                name: '歌手'
              }
            }, {
              name: 'newsongs',
              path: '/newsongs',
              component: NewSongs,
              meta: {
                name: '最新音乐'
              }
            }],
        },
        // 歌单详情列表
        {
          name: 'playlistdetail',
          path: '/playlistdetail/:id?',
          component: PlayDetail,
        },
        // 用户详情信息
        {
          name: 'userdetail',
          path: '/userdetail/:id?',
          component: UserDetail
        },
        // 歌手详情信息
        {
          name: 'artistdetail',
          path: '/artistdetail/:id?',
          component: ArtistDetail
        },
        // 专辑详情信息
        {
          name: 'albumdetail',
          path: '/albumdetail/:id?',
          component: AlbumDetail
        },
        // 视频相关
        {
          name: 'video',
          path: '/video',
          redirect: '/subvideo',
          component: Video,
          children: [
            // 视频
            {
              name: 'subvideo',
              path: '/subvideo',
              component: SubVideo
            },
            // mv
            {
              name: 'submv',
              path: '/submv',
              component: SubMv
            }
          ]
        },
        // 视频详情页
        {
          name: 'videodetail',
          path: '/videodetail/:vid?/:mid?',
          component: VideoDetail
        },
        // 全部MV
        {
          name: 'allmv',
          path: '/allmv/:area?/:type?',
          component: AllMv
        },
        // 全部排行榜
        {
          name: 'alltop',
          path: '/alltop/:area?',
          component: AllTop
        },
        // 私人FM
        {
          name: 'privateFM',
          path: '/privateFM',
          component: PrivateFM
        },
        // 每日推荐
        {
          name: 'dailyspecial',
          path: '/dailyspecial',
          component: DailySpecial
        },
        // 最近播放
        {
          name: 'recentplay',
          path: '/recentplay',
          component: RecentPlay
        },
        // 我的收藏
        {
          name: 'myfavorite',
          path: '/myfavorite',
          component: MyFavorite
        },
        // 搜索详情页
        {
          name: 'searchdetail',
          path: '/searchdetail/:keywords?',
          component: SearchDetail
        }
      ]
    },
    // { name: 'login', path: "/login", component: Login },
  ],
  scrollBehavior (to, from, savedPosition) {
    console.log(to, from, savedPosition)
    // 异步滚动操作
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({ x: 0, y: 1 });
      });
    });
  }
});